package com.example.baselibrary.utils
import android.os.CountDownTimer
import java.math.BigDecimal
object CountDownTimeUtils {
    private var timer: CountDownTimer? = null
    /**
     * 启动倒计时
     * timeAll 倒计时总时长 单位秒
     */
    fun startTimer(timeAll: Long, clickTimer: OnClickTimerListener?) {
        timer?.cancel()
        timer = null
        val allTimer: Long =  timeAll.toBigDecimal() .multiply(BigDecimal.valueOf(1000))?.toLong() ?: 0
        timer = object : CountDownTimer(allTimer, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                val second = (millisUntilFinished / 1000).toInt()
                clickTimer?.onCurrentTime(second)
            }
            override fun onFinish() {
                clickTimer?.onEndTimer()
            }
        }
        timer?.start()
    }

    interface OnClickTimerListener {
        fun onEndTimer()
        fun onCurrentTime(second: Int)
    }

    /**
     * 干掉倒计时
     */
    fun onDestroyTime() {
        timer?.cancel()
        timer = null
    }
}